package com.SmsModule.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import util.Constant;

import com.SmsModule.bean.TbCommonPhrase;
import com.SmsModule.bean.TbSmsSend;
import com.SmsModule.dao.TbSmsTemplateDAO;
import com.UserModule.bean.TbUserBean;
import com.jspsmart.upload.SmartUpload;

public class TbSmsTemplateServlet extends HttpServlet{
	
	public void destroy() {
		super.destroy();
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		TbSmsTemplateDAO smstemplate=new TbSmsTemplateDAO();
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String method = request.getParameter("method");
		TbUserBean tbUser=(TbUserBean)request.getSession().getAttribute("User");	
		//查询所有的常用短语
		if(method.equals("search")){
			String tiaojian=" userId="+tbUser.getUserId();
			
			List  SmsTemplateList=null;
			SmsTemplateList=smstemplate.getTbCommonPhraseAll("tb_CommonPhrase", tiaojian, "createTime");
			request.getSession().setAttribute("SmsTemplateList",SmsTemplateList);
			response.sendRedirect("SmsModule/SmsTemplate.jsp?s=1");
		}else if(method.equals("searchTemplate")){//选择常用短语
		        String fromname=request.getParameter("fromname");
		        String tiaojian=" userId="+tbUser.getUserId();
				List  SmsTemplateList=null;
				SmsTemplateList=smstemplate.getTbCommonPhraseAll("tb_CommonPhrase", tiaojian, "createTime");
				request.getSession().setAttribute("SelectSmsTemplateList",SmsTemplateList);
			
				response.sendRedirect("SmsModule/SelectSmsTemplate.jsp?s=1&fromname="+fromname);
			
		}else if(method.equals("add")){
			String phraseContent=Constant.decodeString(request.getParameter("phraseContent"));
			TbCommonPhrase tbCommonPhrase=new TbCommonPhrase();
			tbCommonPhrase.setPhraseContent(phraseContent);
			tbCommonPhrase.setUserId(tbUser.getUserId());
			tbCommonPhrase.setUseCount(0);
			int insertflag=smstemplate.addTbCommonPhrase(tbCommonPhrase);
			
			if(insertflag>0){
				request.getSession().setAttribute("resultSM", "添加常用短语成功！");
				//out.write("<script language='javascript'>alert('添加常用短语成功！');</script>");
			}else{
				request.getSession().setAttribute("resultSM", "添加常用短语失败！");
				//out.write("<script language='javascript'>alert('添加常用短语失败！');</script>");
			}
			String tiaojian=" userId="+tbUser.getUserId();
			
			List<TbCommonPhrase>  SmsTemplateList=null;
			SmsTemplateList=smstemplate.getTbCommonPhraseAll("tb_CommonPhrase", tiaojian, "createTime");
			
			request.getSession().setAttribute("SmsTemplateList",SmsTemplateList);
			response.sendRedirect("SmsModule/SmsTemplate.jsp?s=1");
			
		}else if(method.equals("update")){
			
			
		}else if(method.equals("deleteOne")){
			String PhraseId=request.getParameter("PhraseId");
			boolean result=smstemplate.del("tb_CommonPhrase", "PhraseId", PhraseId);
			if(result){
				request.getSession().setAttribute("resultSM", "常用短语删除成功！");
				//out.write("<script language='javascript'>alert('常用短语删除成功！');</script>");
			}else{
				request.getSession().setAttribute("resultSM", "常用短语删除失败！");
				//out.write("<script language='javascript'>alert('常用短语删除失败！');</script>");
			}
			String tiaojian=" userId="+tbUser.getUserId();
			
			List  SmsTemplateList=smstemplate.getTbCommonPhraseAll("tb_CommonPhrase", tiaojian, "createTime");
			request.getSession().setAttribute("SmsTemplateList",SmsTemplateList);
			response.sendRedirect("SmsModule/SmsTemplate.jsp?s=1");
		}else if(method.equals("deleteAll")){
			String idList = request.getParameter("idList");
			idList = idList.substring(0, idList.length()-1);
			boolean result=smstemplate.delin("tb_CommonPhrase", "PhraseId", idList);
			if(result){
				request.getSession().setAttribute("resultSM", "常用短语删除成功！");
				//out.write("<script language='javascript'>alert('常用短语删除成功！');</script>");
			}else{
				request.getSession().setAttribute("resultSM", "常用短语删除失败！");
				//out.write("<script language='javascript'>alert('常用短语删除失败！');</script>");
			}
			String tiaojian=" userId="+tbUser.getUserId();
			
			List  SmsTemplateList=smstemplate.getTbCommonPhraseAll("tb_CommonPhrase", tiaojian, "createTime");
			request.getSession().setAttribute("SmsTemplateList",SmsTemplateList);
			response.sendRedirect("SmsModule/SmsTemplate.jsp?s=1");
		}
		
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
	
	public void init() throws ServletException {
	}
}
